var CN_COMMON_ARTICLE_INDEX = function(){
	this.init();
};
CN_COMMON_ARTICLE_INDEX.prototype.init = function(){
	var self = this;
	self.expand();
	
	self.initSomeThing();
	self.bandClick();
	self.bandResize();
}
/**
 * 拓展方法
 */
CN_COMMON_ARTICLE_INDEX.prototype.expand = function(){
	var self = this;
	self.initSomeThing = function() {
		self.initScroll();
	};
	self.bandClick = function() {
		
	};
	self.bandResize = function() {
	};
	self.initScroll = function() {
		// 滚动条总共高度(scrollHeight) = 滚动过的高度(scrollTop) + 屏幕的可见高度(windowHeight)
		var scrollHeight = document.body.scrollHeight || document.documentElement.scrollHeight;
		var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
		var flag = true;
		var pageNo = 1;
		window.onscroll = function() {
		    var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
		    if(scrollHeight - (scrollTop + windowHeight) <= 50 && flag) {
		        $("#loading").show();
		        flag = false;
		        $.get(ctx + 'front/article/load/' + (pageNo++) + '.html', function(datas) {
		            var html = [];
		            var article = {};
		            for (var i = 0; i < datas.length; i++) {
		            	article = datas[i];
		            	html.push('<li id="' + article.id + '" class="group">');
			            html.push('    <div class="item-main">');
			            html.push('        <div class="title">');
			            html.push('            <span class="myh2">');
			            if(currentUserId == article.createUserId) {
			            	html.push('            <span style="cursor:pointer" title="删除此文章" onclick="cnCommonArticleIndex.del(\'' + article.id + '\')"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></span>');
			            	html.push('            <span style="cursor:pointer" title="编辑此文章" onclick="cnCommonArticleIndex.edit(\'' + article.id + '\')"><i class="fa fa-trash-o" aria-hidden="true"></i></span>');
			            }
			            var articleTitle = article.articleTitle;
			            if(articleTitle && articleTitle.length > 100) {
			            	articleTitle = articleTitle.substring(0, 100) + "...";
			            }
			            html.push('                <a target="_blank" href="' + ctx + 'front/article/detail/' + article.id + '.html" rel="bookmark" title="' + articleTitle + '">' + articleTitle + '</a>');
			            html.push('            </span>');
			            html.push('        </div>');
			            html.push('        <div class="meta">');
			            html.push('            <div class="cat">');
			            html.push('                <a href="" rel="category tag">' + article.articleCategoryName + '</a>');
			            html.push('                <span class="tag-links">');
			            html.push('                    <i>');
			            var articleLabel = article.articleLabel;
			            if(articleLabel) {
			                var labels = articleLabel.split(',');
			                for(var k = 0; k < labels.length; k++) {
			                    var label = labels[k];
			                    if(label && label != "null") {
			                    	html.push('<a href="" rel="tag">' + label + '</a>');
			                        if(k < labels.length - 1) {
			                        	html.push('-');
			                        }
			                    }
			                }
			            }
			            html.push('                    </i>');
			            html.push('                </span>');
			            html.push('            </div>');
			            html.push('            <div class="excerpt">' + article.articleIntroduction + '</div>');
			            html.push('                <div class="data">');
			            html.push('                    <time class="time"><i class="fa fa-calendar"></i> ' + article.createUserName + ' 发布于 ' + article.verifyTimeFmt + '</time>');
			            html.push('                    <span class="heart-num"><i class="fa fa-heart"></i>' + article.articleCollectNum + '</span>');
			            html.push('                    <span><i class="fa fa-fire"></i>' + article.articleClickNum + '</span>');
			            html.push('                    <span class="comment-num"><a href="" class="comments-link"><i class="fa fa-comment"></i>' + article.articleCommentNum + '</a></span>');
			            html.push('                </div>');
			            html.push('                <div class="author">');
			            html.push('                    <a target="_blank" href="" title="' + article.createUserId + ' 的主页" rel="author">');
			            html.push('                        <img src="' + ctx + 'statics/page/image/front/article/article.author.png" class="avatar" alt="' + article.createUserName + '" width="40" height="40"/>'); 
			            html.push('                    </a>');
			            html.push('                </div>');
			            html.push('            </div>');
			            html.push('        </div>');
			            html.push('    </div>');
			            html.push('</li>');
		            }
		            html.push('<li style="text-align: center" id="loading"> <img alt="" src="<%=request.getContextPath()%>/static/site/image/ajax-loader.gif"> </li>');
		            $('#articleListWrap').append(html.join(''));
		            scrollHeight = document.body.scrollHeight || document.documentElement.scrollHeight;
		            flag = true;
		            $("#loading").hide();
		        }, 'json')
		    } 
		}
	};
};
//供外部调用的方法
CN_COMMON_ARTICLE_INDEX.prototype.edit = function(articleId) {
	window.open(ctx + "front/article/edit/" + articleId + ".html"); 
};
CN_COMMON_ARTICLE_INDEX.prototype.del = function(articleId) {
	showConfirm("是否要删除此文章？", function() {
		var loadingIndex = submitHolder();
		jsonAjax("front/article/delete/" + articleId + ".html", 'json', '', function(data) {
			closeSubmitHolder(loadingIndex);
			closeAll();
			if(data.status == SysConstant.SUCCESS) {
				tipsSuccess(data.msg);
				window.location.reload()
			} else {
				tipsFailed(data.msg);
			} 
		});
	}, function() {
		
	});
};
$(function() {
	window.cnCommonArticleIndex = new CN_COMMON_ARTICLE_INDEX();
});
